<?php
class Login extends MY_Controller{
	function __construct(){
		parent::__construct();
		$this->folder = LOGIN_FOLDER;
		$this->mainmodel = $this->mClient;
		$this->postConstructor();
	}
	
	function postConstructor(){
		if($this->folder== "admin" || $this->folder == "developer"){
			if($this->session->userdata(ADMIN_USER_ID ) != ""){
					$this->page = $this->session->userdata(DEFAULT_ADMIN_PAGE );
					$this->folder = $this->session->userdata(DEFAULT_ADMIN_FOLDER );
					$this->redirecttorecordlist();
			}
		}
	}
	
	public function index(){
		
		//echo 'LOGIN FOLDER----'.LOGIN_FOLDER;
		if($this->folder == "admin" || $this->folder == "developer"){
			$this->folder = "admin";
			$this->mainmodel = $this->mDevUsers;
		}
		
		$session_id = $this->session->userdata('session_id');
		$form_uname = "u"."$session_id"."n";
		$form_pword = "p"."$session_id"."w";
		
		$USERNAME = $this->input->post($form_uname,TRUE);
		$PASSWORD = $this->input->post($form_pword,TRUE);
		
		//echo "($USERNAME)-----------------($PASSWORD)";
		if($USERNAME != "" && $PASSWORD != ""){
			//ECHO "HEREEE";
			$this->load->library('form_validation');
			$this->form_validation->set_value($form_uname);
			$this->form_validation->set_rules($form_uname, humanize("Username"), "trim|xss_clean|required|callback_validate_login");
			
			$this->form_validation->set_value($form_pword);
			$this->form_validation->set_rules($form_pword, humanize("Password"), "trim|xss_clean|required");
			$this->validationResult = $this->form_validation->run();
		//echo "VALIDATION RESULT ::: ".$this->form_validation->run() ;
			
				
		}
		$data = array();
		$this->loadPage($data);
	}
	
	function validate_login(){
		//echo "VALIDATE LOGIN WAS CALLED";
		$session_id = $this->session->userdata('session_id');
		$form_uname = "u"."$session_id"."n";
		$form_pword = "p"."$session_id"."w";
		
		$USERNAME = $this->input->post($form_uname,TRUE);
		$PASSWORD = $this->input->post($form_pword,TRUE);
		$user = $this->mainmodel->login( $USERNAME , $PASSWORD );
		
		if($this->folder == "admin" || $this->folder == "developer"){
			if($user != null){
				$this->session->set_userdata(ADMIN_USER_ID , $user['id']);
				$ALLOWEDPAGES = $this->mDevPagePermission->loadallowedpages();
				forEach($ALLOWEDPAGES as $page){
					$this->page = $page['pagename'];
					$this->session->set_userdata(DEFAULT_ADMIN_PAGE , $this->page);
					$this->session->set_userdata(DEFAULT_ADMIN_FOLDER , $this->folder );
					$this->redirecttorecordlist();
				}
			}
		}else{
			if($user != null){
				$this->session->set_userdata(CLIENT_USER_ID , $user['id']);
				REDIRECT(base_url());
			}
		}
		
	}
	
	function loadPage($data = array()){
		$data['action'] = $this->action;
		$data['page'] = $this->page;
		$data['nextaction'] = $this->nextaction;
		$data['validationResult'] = $this->validationResult;
		$data['CAPTION'] = humanize($this->page);
		$this->parser->parse("/".$this->folder.'/login', $data);
		$this->benchmark->mark('end');
	}
	
	
}